<template>
  <div class="app-container">
    <div style="display:flex;justify-content:space-between;align-items: center;margin-top: 20px;">
      <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
        <el-form-item label="姓名" prop="realName">
          <el-input v-model="queryParams.realName" placeholder="请输入员工姓名" clearable size="small" style="width: 150px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item label="工号" prop="userSn">
          <el-input v-model="queryParams.userSn" placeholder="请输入员工工号" clearable size="small" style="width: 150px"
            @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleQuery">搜索</el-button>
          <el-button size="small" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <div style="text-align:right;margin-bottom: 22px;">
        <el-button-group slot="reference">
          <el-button size="small" icon="el-icon-refresh" @click="getDetail" />
          <el-button size="small" icon="el-icon-plus" @click="listVisible = true">添加</el-button>
        </el-button-group>
      </div>
    </div>

    <el-table v-loading="loading" border :data="pageList" @sort-change="sortChange">
      <el-table-column label="编号" prop="id" width="50" align="center" />
      <el-table-column label="房间名称" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{ roomName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="realName" />
      <el-table-column label="工号" align="center" prop="userSn"></el-table-column>
      <el-table-column label="远程开门" align="center" prop="remoteOpen">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.remoteOpen" :active-value="true" :inactive-value="false"
            @change="handleRemoteOpenChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="添加时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete"
            @click="removeUser(scope.row, scope.$index)">移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 选择员工 -->
    <el-dialog v-dialogDrag class="form_dialog" title="选择员工" :visible.sync="listVisible" width="800px" top="5vh"
      append-to-body :close-on-click-modal='false'>
      <studentList v-if="listVisible" @checkList="getStudentList"></studentList>
      <div slot="footer">
        <el-button @click="listVisible = false">关 闭</el-button>
        <el-button type="primary" @click="checkItems">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRoomDetail,
  addGateUser,
  delGateUser,
  changeUserRemote,
} from "@/api/gate/auth";
import studentList from './components/studentList.vue';

export default {
  components: { studentList },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房间表格数据
      userList: [],
      pageList: [],
      // 是否显示弹出层
      listVisible: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 20,
        realName: undefined,
        userSn: undefined,
      },
      roomId: undefined,
      roomName: undefined,

      // 选择的员工list
      studentList: [],
    };
  },
  created() {
    this.roomId = this.$route.params.roomId
    this.getDetail();
  },
  methods: {
    /** 查询房间详情 */
    getDetail() {
      this.loading = true;
      getRoomDetail(this.roomId).then(
        response => {
          this.userList = response.data.userList || [];
          this.pageList = this.userList
          this.total = this.userList.length;
          this.roomName = response.data.name
          this.loading = false;
        }
      );
    },
    getList() {
      let qName = this.queryParams.realName
      let qSn = this.queryParams.userSn
      let list = this.userList

      if (qName || qSn) {
        list = this.userList.filter((user) => {
          if (qName && user.realName && user.realName.includes(qName)) {
            return true
          }
          if (qSn && user.userSn && user.userSn.includes(qSn)) {
            return true
          }
          return false
        })
      }
      let start = (this.queryParams.pageNo - 1) * (this.queryParams.pageSize)
      let end = (this.queryParams.pageNo) * (this.queryParams.pageSize)

      this.pageList = list.slice(start, end)
    },
    sortChange(column, prop, order) {
      if (column.order === 'ascending') {
        this.queryParams.orderBy = column.prop + ' ' + 'asc'
      } else if (column.order === 'descending') {
        this.queryParams.orderBy = column.prop + ' ' + 'desc'
      } else {
        this.queryParams.orderBy = ''
      }
      this.getList()
    },
    /**远程权限改变 */
    handleRemoteOpenChange(row) {
      if (!this.roomId || !row.id) {
        return;
      }

      changeUserRemote(this.roomId, row.id, row.remoteOpen).then(res => {
        console.log(this.roomId + ',' + row.id + ',' + res)
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    //移除门禁人员
    removeUser(row, index) {
      let rId = this.roomId
      if (!rId) {
        return
      }
      this.$confirm('是否确认移除编号为"' + row.id + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delGateUser(rId, row.id)
      }).then(() => {
        this.pageList.splice(index, 1)
        this.msgSuccess("移除成功");
      })
    },
    // 选择学生数据
    getStudentList(list) {
      this.studentList = list
    },
    //保存选择的员工
    checkItems() {
      if (!this.studentList.length) {
        return
      }

      let ids = this.studentList.map(item => item.userId)
      let remoteOpenList = this.studentList.map(item => item.remoteOpen)
      addGateUser(this.roomId, ids, remoteOpenList).then(
        response => {
          this.msgSuccess("保存成功");
          this.listVisible = false
          this.getDetail();
        }
      )
    },
  }
};
</script>
